/**
  ******************************************************************************
  * @file           : TouchPad.cpp
  * @author         : admin
  * @brief          : None
  * @attention      : None
  * @date           : 2024/10/11
  ******************************************************************************
  */

#include "TouchPad.h"
TouchPad::TouchPad(uint8_t _io)
        : touchedCb(NULL), untouchedCb(NULL), io(_io), valuePrevPrev(80), valuePrev(80), value(80), threshold(35)
{
}

uint32_t TouchPad::read(void)
{
    if ((touchedCb == NULL) && (untouchedCb == NULL))
    {
        return touchRead(io);
    }
    return value;
}

bool TouchPad::isTouched(void)
{
    valuePrevPrev = valuePrev;
    valuePrev = value;
    value = touchRead(io);
    //if(value < threshold )
    //    Serial.printf("--touch io=%d----valuePrevPrev=%d--valuePrev=%d---value=%d---\n",io,valuePrevPrev,valuePrev,value);
    return ((value < threshold) && (valuePrev < threshold) && (valuePrevPrev < threshold));
}

void TouchPad::setTouchedCallback(CBFunc cb)
{
    static char name[] = {"touchX"};
    name[5] = io + '0';
    if ((touchedCb == NULL) && (untouchedCb == NULL))
    {
        xTaskCreatePinnedToCore(TouchPad::taskLoop, name, 2048, this, 1, NULL, ARDUINO_RUNNING_CORE);
    }
    touchedCb = cb;
}

void TouchPad::setUnTouchedCallback(CBFunc cb)
{
    static char name[] = {"touchX"};
    name[5] = io + '0';
    if ((touchedCb == NULL) && (untouchedCb == NULL))
    {
        xTaskCreatePinnedToCore(TouchPad::taskLoop, name, 2048, this, 1, NULL, ARDUINO_RUNNING_CORE);
    }
    untouchedCb = cb;
}
TouchPad touchPadP(27), touchPadY(14), touchPadT(12), touchPadH(13), touchPadO(15), touchPadN(4);